package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.DatabaseException;
import model.LoginModel;

/**
 * Servlet implementation class UserCheck
 */
@WebServlet("/UserCheck")
public class UserCheck extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("name");
//		System.out.println("UserCheckServlet: Checking Username! "+username);
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		boolean available = false;
		try {
			available = LoginModel.isLoginNameAvailable(username);
//			System.out.println(available);
			
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		if(available){
			response.getWriter().print("1");
		}else{
			response.getWriter().print("0");
		}
	}

}
